package com.yjx.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjx.dao.RoleMapper;
import com.yjx.entity.Role;
import com.yjx.service.OperateTokenService;
import com.yjx.service.RoleService;
import com.yjx.service.UserTokenService;
import com.yjx.vo.RoleVo;
import com.yjx.vo.UserVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Role)表服务实现类
 *
 * @author makejava
 * @since 2023-04-16 11:21:39
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private OperateTokenService operateTokenService;
    @Resource
    private UserTokenService userTokenService;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Role queryById(Integer id) {
        return this.roleMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Role> queryAllByLimit(int offset, int limit) {
        return this.roleMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Role insert(Role role) {
        this.roleMapper.insert(role);
        return role;
    }

    /**
     * 修改数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Role update(Role role) {
        this.roleMapper.update(role);
        return this.queryById(role.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.roleMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean checkRoleCode(String roleCode) {
        Role role = roleMapper.queryByRoleCode(roleCode);
        if (role==null){
            return true;
        }
        return false;
    }

    @Override
    public Integer saveRole(Role role,String token,String operateToken) {
        //验证operateToken
//        String value = redisUtil.get(operateToken);
        //去redis中查询
        //没有查询到结果
        Boolean aBoolean = operateTokenService.checkOperateToken(operateToken);
        if (aBoolean){
            return null;
        }
        //查询到了
        //从redis中移除
//        redisUtil.delete(operateToken);
        //根据token获取用户信息
        String userJons = userTokenService.getUserByToken(token);
        JsonMapper jsonMapper = new JsonMapper();
        Date date = new Date();
        try {
            UserVo userVo = jsonMapper.readValue(userJons, UserVo.class);
            role.setModifyUser(userVo.getId());
            role.setModifyTime(date);
            role.setCreateTime(date);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return roleMapper.insert(role);
    }

    @Override
    public Integer updateIsDeleteById(Integer id) {
        return roleMapper.updateIsDeleteById(id);
    }

    @Override
    public List<RoleVo> queryAllRole() {
        return roleMapper.queryAllRole();
    }

    @Override
    public PageInfo<RoleVo> queryRoleByRoleName(String roleName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<RoleVo> roleVoList = roleMapper.queryRoleByRoleName(roleName);
        PageInfo<RoleVo> pageInfo = new PageInfo<>(roleVoList);
        return pageInfo;
    }


}
